function ScnPrefsAssistant(argFromPusher) {
}

ScnPrefsAssistant.prototype = {
	setup: function() {
		var PrefsCookie = new Mojo.Model.Cookie('PrefsCookie');
		var PrefsData = PrefsCookie.get();
		if (PrefsData !== undefined) {
			this.txtLatModel = {value : PrefsData.myLat};
			this.txtLonModel = {value : PrefsData.myLon};
			this.txtMaxDistModel = {value : PrefsData.myMaxDist};
			this.sliMaxDistModel = {value : PrefsData.myMaxDist};
			this.sliMagModel = {value : PrefsData.myMag};
			this.lblMagNbModel = {label : PrefsData.myMag + "+"};
			this.sliRefreshModel = {value : PrefsData.myPageRefresh};
			this.lblRefreshNbModel = {label : PrefsData.myPageRefresh + " min."};
			this.liseDistModel = {value : PrefsData.myDist};
			this.cbInfoModel = {value : PrefsData.myShowInfo};
			this.cbOverrideModel = {value : PrefsData.myOverride};
		}
		this.lblMagModel = {label : $L("Minimum Magnitude")};
		this.lblMaxDistModel = {label : $L("Maximum Distance to Epicenter")};
		this.lblRefreshModel = {label : $L("Refresh data every")};
		this.lblInfoModel = {label : $L("Display data on map")};
		this.lblMyLoc = {label : $L({value : "If GPS is switched off or not able to fix my position, use the following coordinates to calculate distance to epicenter:", key : "lblMyLoc"})};
		this.lblLatModel = {label : $L("Latitude")};
		this.lblLonModel = {label : $L("Longitude")};
		this.btnGetMyLocModel = {label : $L("Get it from GPS")};
		this.lblOverrideModel = {label : $L("Overwrite those values each time a location fix is performed successfully")};
		Ares.setupSceneAssistant(this);
		this.controller.setupWidget(Mojo.Menu.appMenu,
			this.attributes = {
				omitDefaultItems: true
			},
			this.model = {
				visible: true,
				items: [ 
					Mojo.Menu.editItem,
					{label: $L('Reset Preferences'), icon: "delete", command: "do-KillCookie"},
					// { label: $L("Help..."), command: 'do-myHelp' },
					{ label: $L("About SeismiX..."), command: 'do-myAbout'}
				]
			}
		);
		this.$.hdrPrefs.setTitle($L("Preferences"));
		this.$.liseDist.setLabel($L("Display distance in"));
		this.$.collFilter.setTitleLabel($L("Filter Options"));
		this.$.collDisplay.setTitleLabel($L("Display Options"));
		this.$.collOtherOpt.setTitleLabel($L("Other Options"));
		this.$.grpMyLoc.setLabel($L("My Location"));
	},

	handleCommand: function(event) {
		if (event.type === Mojo.Event.command) {
			if (event.command == "do-myAbout") {
				this.controller.stageController.pushScene("scnAbout");
			}
			if (event.command == 'do-KillCookie') { 
				try {
					var PrefsCookie = new Mojo.Model.Cookie('PrefsCookie');
					PrefsCookie.remove();
					this.$.txtLat.setValue(0);
					this.$.txtLon.setValue(0);
					this.$.sliMag.setValue(3);
					this.$.lblMagNb.setLabel("3+");
					this.$.sliRefresh.setValue(10);
					this.$.lblRefreshNb.setLabel("10 min.");
					this.$.liseDist.setValue("K");
					this.$.cbInfo.setValue(1);
					this.$.sliMaxDist.setValue(20037);
					this.$.txtMaxDist.setValue(20037);
					this.$.cbOverride.setValue(1);
					Mojo.Controller.getAppController().showBanner({messageText: $L("Preferences resetted"), icon: "images/day-event-info-press.png"},{source: 'notification'}, "SeismiX");
				}
				catch(err) {
					Mojo.Controller.errorDialog($L("Could not reset preferences."));
				}
			}
			if (event.command == "do-myHelp") {
			}
		}
	},

	cleanup: function() {
		var PrefsCookie = new Mojo.Model.Cookie('PrefsCookie');
		try {
			PrefsCookie.put({
				myLat: this.txtLatModel.value,
				myLon: this.txtLonModel.value,
				myMag: this.sliMagModel.value,
				myPageRefresh: this.sliRefreshModel.value,
				myDist: this.liseDistModel.value,
				myMaxDist : this.txtMaxDistModel.value,
				myShowInfo: this.cbInfoModel.value,
				myOverride: this.cbOverrideModel.value
			});
		}
		catch (err) {
			Mojo.Log.error(" ### Could not write preferences cookie.");
			Mojo.Controller.errorDialog($L("Saving the preferences failed."));
		}
		this.controller.stageController.parentSceneAssistant(this).needRefresh = true;
		this.controller.stageController.parentSceneAssistant(this).currPrefs.pageRefresh = this.sliRefreshModel.value;
		Ares.cleanupSceneAssistant(this);
	},
	

	sliMagChange: function(inSender, event) {
		this.lblMagNbModel.label = event.value + "+";
		this.controller.modelChanged(this.lblMagNbModel);
	},

	sliRefreshChange: function(inSender, event) {
		this.lblRefreshNbModel.label = event.value + " min.";
		this.controller.modelChanged(this.lblRefreshNbModel);
	},

	gps1Success: function(inSender, inResponse, inRequest) {
		if (inResponse !== 'undefined') {
			this.$.txtLat.setValue(inResponse.latitude);
			this.$.txtLon.setValue(inResponse.longitude);
			this.controller.get('btnGetMyLoc').mojo.deactivate();
		}	
	},

	gps1Failure: function(inSender, inError) {
		var errStr = $L("Could not get a location fix!") + " ";
		switch (inError.errorCode) {
			case  1: //Timeout
				errStr = errStr + $L("The GPS/LocationService timed out.");
				break;
			case 5: // LocationServiceOFF - No Location source available. Both Google and GPS are off.
				errStr = errStr + $L("No Location source available. Both Google and GPS are off.");
				break;
			case 6: // Permission Denied - The user has not accepted the terms of use for the Google Location Service, or the Google Service is off.
				errStr = errStr + $L("The user has not accepted the terms of use for the Google Location Service.");
				break;
			case 8: // The application has been temporarily blacklisted.
				errStr = errStr + $L({value : "The application has been temporarily blacklisted. Please switch 'Auto Locate' on in the Location Services app.", key : "Blacklisted"});
				break;
		}
		this.controller.showAlertDialog({
			preventCancel: false,
			onChoose: function(value) {
				this.controller.get('btnGetMyLoc').mojo.deactivate();
				if (value == 'LocSvc') {
					this.controller.serviceRequest('palm://com.palm.applicationManager', {
						method: 'launch',
						parameters:  {
							id: 'com.palm.app.location',
							params: {}
						}
					});
				}
			},
			title: $L("No GPS available"),
			message: errStr,
			choices:[
				{label:$L('Open Location Services'), value:"LocSvc", type:'affirmative'},
				{label:$L('Continue without GPS'), value:"GoOn", type: 'dismiss'}
			]
		});
	},

	btnGetMyLocTap: function(inSender, event) {
		this.controller.get('btnGetMyLoc').mojo.activate();
		this.$.gps1.getCurrentPosition();
	},
	sliMaxDistChange: function(inSender, event) {
		this.txtMaxDistModel.value = event.value;
		this.controller.modelChanged(this.txtMaxDistModel);
	}
};